<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/04/22
 */
namespace common\logic;

use yii;
use common\models\Occupancy as OccupancyModel;
use common\models\Order;
use common\tool\Currency;

class Occupancy {
    public static function getTripPrice($tripId) {
    }

    public static function getTripCabinsPrice($tripId, $cabinIds = []) {
        if (empty($tripId)) return false;
        if (empty($cabinIds)) {

        }
        $cabinIdsStr = implode(',', $cabinIds);
        $r = OccupancyModel::getList("trip_id=$tripId AND cabin_id IN ($cabinIdsStr)", 'cabin_id,price','price DESC');
        $map = [];
        foreach ($r as $k => $v) {
            $map[$v['cabin_id']] = ceil(Currency::eur2cnyYuan($v['price']));
        }
        return $map;
    }

    public static function getTripsPrice($tripIds = []) {
        if (empty($tripIds)) return [];
        $tripIds_str = implode(',', $tripIds);
        $r = OccupancyModel::getList("trip_id IN ($tripIds_str)", 'trip_id,price', 'price DESC');
        $res = [];
        foreach ($r as $k => $v) {
            $res[$v['trip_id']] = ceil(Currency::eur2cnyYuan($v['price']));
        }
        return $res;
    }

    public static function getBoatsPrice($boatIds = []) {
        if (empty($boatIds)) return [];
        $map = [];
        $keys = array_map(function($v) { return CacheKey::boatPrice($v); }, $boatIds);
        $cache = Yii::$app->cache->multiGet($keys);
        foreach ($cache as $k => $v) {
            list($boatId) = CacheKey::boatPriceReverse($k);
            $map[$boatId] = $v;
        }
        $missKeys = array_keys(array_filter($cache, function($v) { return $v === false; }));
        if ($missKeys) {
            $needCache = [];
            foreach ($missKeys as $key) {
                list($boatId) = CacheKey::boatPriceReverse($key);
                $sql = "SELECT min(b.price) FROM dive_trip as a join `dive_occupancy` as b on a.id=b.trip_id where a.boat_id=$boatId";
                $tmp = OccupancyModel::getDb()->createCommand($sql)->queryScalar();
                $needCache[$key] = $map[$boatId] = ceil(Currency::eur2cnyYuan($tmp));
            }
            Yii::$app->cache->multiSet($needCache, 300);
        }
        return $map;
    }

    public static function getOccupancies($tripId) {
        return OccupancyModel::getList("trip_id=$tripId");
    }
    public static function getOccupanciesByTripCabin($tripId,$cabinId){
        $occupancies = OccupancyModel::getList("trip_id=$tripId and cabin_id=$cabinId");
        return $occupancies;
    }

    public static function chooseOccupancy($tripId, $cabinId, $oneSelf = Order::NOT_ONE_SELF) {
        if (empty($tripId)) return false;
        $condition = "trip_id=$tripId";
        $cabinId && $condition .= " AND cabin_id=$cabinId";
        if ($oneSelf) {
            $condition .= " AND name_en='" . OccupancyModel::SINGLE . "'";
        } else {
            $condition .= " AND name_en<>'" . OccupancyModel::SINGLE . "'";
        }
        $r = OccupancyModel::getList($condition, '*', 'price DESC', 0, 1);
        return $r[0]??false;
    }
}
